--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once

function IsInOpenServerEquipGoldRecoverDay()
	local startDay = OpenServerCfg.GoldRecover.openDay or 1
	local endDay = OpenServerCfg.GoldRecover.endDay or OpenServerCfg.OpenServerDay
	local openday = System.getDaysSinceOpenServer()
    if openday >= startDay and openday <= endDay then
        return true
    end
    return false
end

--根据id获取配置或者变量
function OpenServerGoldRecoverGetCfgAndVarById(sysarg)
	local aVar = Actor.getStaticVar(sysarg)
	local sVar = System.getStaticVar()
	if not aVar.openserverGoldRecover then
		aVar.openserverGoldRecover = {}
	end
	if not sVar.openserverGoldRecover then
		sVar.openserverGoldRecover = {}
	end

	local actCfg = OpenServerCfg.GoldRecover
	
	return actCfg,aVar.openserverGoldRecover,sVar.openserverGoldRecover
end

-- 打包数据
function SendOpenServerEquipGoldRecoverData(sysarg)
	if not IsInOpenServerEquipGoldRecoverDay() then
		return
	end
 	local config, playerState, serverCount = OpenServerGoldRecoverGetCfgAndVarById(sysarg)

 	local npack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sOpenServerGoldRocoverData)
	if not npack  then
		return
	end
	local len = #config.Awards
	DataPack.writeByte(npack, len)
	for idx = 1, len do
		local cfg = config.Awards[idx]
		local state = playerState[idx] or 0
		local useCount = serverCount[idx] or 0
		local lastCount = cfg.maxCount - useCount 
		if cfg.maxCount == -1 then
			lastCount = -1
		end
		--print("lastCount = "..lastCount)
		--print("state = "..state)
		DataPack.writeByte(npack, state)
		DataPack.writeShort(npack, lastCount)
	end

    local dVar = Actor.getDyanmicVar(sysarg)
    local circle1 = dVar.os_goldrecover_h_circle or 1
    local index1 = dVar.os_goldrecover_h_index or 1

    local circle2 = dVar.os_goldrecover_n_circle or 1
    local index2 = dVar.os_goldrecover_n_index or 1

    local dataHigh, curCicle1, curIndex1 = getRecordGlobalTableData(RecordTableIndexDefine.OSGoldRecoverHigh, circle1, index1, config.logHighCount)
    dVar.os_goldrecover_h_circle =  curCicle1
    dVar.os_goldrecover_h_index =  curIndex1
    if dataHigh then
        local len = #dataHigh
        --print("len ="..len)
        DataPack.writeByte(npack, len)
        for idx = len, 1, -1 do
            --print("idx = "..idx)
            --print("dataHigh[idx].param ="..dataHigh[idx].param)
            --print("dataHigh[idx].param2 ="..dataHigh[idx].param2)
            --print("dataHigh[idx].param3 ="..dataHigh[idx].param3)
            DataPack.writeString(npack, dataHigh[idx].param)
            DataPack.writeShort(npack, dataHigh[idx].param2)
            DataPack.writeInt(npack, dataHigh[idx].param3)
        end
    else
        --print("len = 0")
        DataPack.writeByte(npack, 0)
    end

    local dataNormal, curCicle2, curIndex2 = getRecordGlobalTableData(RecordTableIndexDefine.OSGoldRecoverNormal, circle2, index2, config.logNormalCount)
    dVar.os_goldrecover_n_circle =  curCicle2
    dVar.os_goldrecover_n_index =  curIndex2
    if dataNormal then
        local len = #dataNormal
        --print("len ="..len)
        DataPack.writeByte(npack, len)
        for idx = len, 1, -1 do
            --print("idx = "..idx)
            --print("dataNormal[idx].param ="..dataNormal[idx].param)
            --print("dataNormal[idx].param2 ="..dataNormal[idx].param2)
            --print("dataNormal[idx].param3 ="..dataNormal[idx].param3)
            DataPack.writeString(npack, dataNormal[idx].param)
            DataPack.writeShort(npack, dataNormal[idx].param2)
            DataPack.writeInt(npack, dataNormal[idx].param3)
        end
    else
        --print("len = 0")
        DataPack.writeByte(npack, 0)
    end

    DataPack.flush(npack) 
    
end

function GetOpenServerEquipGoldRecoverAward(sysarg, packet)
	if not IsInOpenServerEquipGoldRecoverDay() then
		return
	end
    local config, aAwardVar, sHadSendCountVar = OpenServerGoldRecoverGetCfgAndVarById(sysarg)
    local index = DataPack.readByte(packet)
    --print("index ="..index)
	if not config then
		return
	end

	local subAwards = config.Awards[index]
	if  not subAwards  then
		return
	end
	local openday = System.getDaysSinceOpenServer()
	local startDay = subAwards.openDay or OpenServerCfg.GoldRecover.openDay
	local endDay = subAwards.endDay or OpenServerCfg.GoldRecover.endDay
	startDay = startDay or 1
	endDay = endDay or OpenServerCfg.OpenServerDay
	if openday < startDay or openday > endDay then
		return
	end
	if not Actor.checkActorLevel( sysarg, OpenServerCfg.GoldRecover.levelLimit[2], OpenServerCfg.GoldRecover.levelLimit[1] ) then
		Actor.sendTipmsg( sysarg, string.format(Lang.ScriptTips.OpenServerGoldRecoverTips001,OpenServerCfg.GoldRecover.levelLimit[2]), ttFlyTip )
		return
	end
	local hadSendCount = sHadSendCountVar[index] or 0
	if subAwards.maxCount == -1 or hadSendCount < subAwards.maxCount then --可以领
		local awardState = aAwardVar[index] or OpenServerAwardState_No_Complete
		if awardState == OpenServerAwardState_No_Complete then
			local consumes = {}
			local needCount = subAwards.nNeedCount or 1
			if needCount <= 0 then
				return
			end
			for idx = 1, #subAwards.idList do
				local count = Actor.getItemCount(sysarg, subAwards.idList[idx]) 
				if count > 0 then
					local consume = OpenServerGoldRecoverClone(subAwards.consume)
					consume.id = subAwards.idList[idx]
					if needCount <= count then
						consume.count = needCount
						table.insert(consumes, consume)
						break
					else
						consume.count = count
						table.insert(consumes, consume)
					end
				end
			end
			if #consumes == 0 then
				return
			end
			if not CheckConsumeCond( sysarg, consumes, 1 ) then
				return 
			end
			if not Awards.CheckBagGridCount(sysarg, subAwards.awards) then
				Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
				return
			end
			if not DoConsumeCond( sysarg, consumes, 1, LogIds.OpenServerGoldRecoverLogId, Lang.LogTips.OpenServerGoldRecover ) then
				return
			end
			Awards.Give(sysarg,subAwards.awards,LogIds.OpenServerGoldRecoverLogId,Lang.LogTips.OpenServerGoldRecover)
			sHadSendCountVar[index] = hadSendCount + 1
			aAwardVar[index] = OpenServerAwardState_Complete_Had_Award
			local goldNum = 0
			for iii = 1, #subAwards.showAwards do
				if subAwards.showAwards[iii].type == qatYuanbao then
					goldNum = subAwards.showAwards[iii].count
					break
				end
			end
			if subAwards.BroadInfo and subAwards.BroadInfo ~= "" then
				local linkMsg = ""
				for ii = 1, #consumes do
					local consume = consumes[ii]
					local itemId = consume.id or 0
					if linkMsg ~= "" then
						linkMsg = linkMsg .. "、"
					end
					linkMsg = linkMsg .. Item.getItemLinkMsg(itemId)
				end
				--print("linkMsg ="..linkMsg)
				--print("goldNum ="..goldNum)
				--print("enLinkOSEquipGoldRecoverWnd ="..enLinkOSEquipGoldRecoverWnd)
				local noticeStr = string.format(subAwards.BroadInfo, Actor.getName(sysarg), linkMsg, goldNum, enLinkOSEquipGoldRecoverWnd)
				System.broadcastTipmsg(noticeStr, ttScreenCenter + ttChatWindow)
			end
			local logMaxCount = OpenServerGoldRecoverGetValueByType(subAwards.logType, config)
			local saveData = {}
			saveData.param = Actor.getName(sysarg)
			saveData.param1 = 0
			saveData.param2 = consumes[1].id or 0             --暂时取1，到时候在优化咯
			saveData.param3 = goldNum
			setRecordGlobalTableData(subAwards.logType, saveData, logMaxCount)
			
			SendOpenServerEquipGoldRecoverData(sysarg)
		end
	elseif hadSendCount >= subAwards.maxCount then
		SendOpenServerEquipGoldRecoverData(sysarg)
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServer006, ttFlyTip)
		return
	end
   
end


function OpenServerGoldRecoverGetValueByType(logType, config) 
	if logType == RecordTableIndexDefine.OSGoldRecoverNormal then
		return config.logNormalCount
	elseif logType == RecordTableIndexDefine.OSGoldRecoverHigh then
		return config.logHighCount
	end
	return config.logNormalCount
end

function OpenServerGoldRecoverClone(award)
	local newAward={}
	newAward.type 				= award.type
	newAward.id 				= award.id
	newAward.count 				= award.count
	newAward.quality 			= award.quality
	newAward.strong				= award.strong
	newAward.bind				= award.bind
	newAward.qualityDataIndex	= award.qualityDataIndex
	return newAward
end 
